package com.solcoo.customer.model;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;

import com.alibaba.fastjson.JSON;
import com.android.common.App;
import com.android.common.network.HttpDownloader;
import com.android.common.network.HttpResult;
import com.android.common.util.DateUtil;
import com.android.common.util.DialogUtil;
import com.android.common.util.StringUtil;
import com.solcoo.customer.ApplicationExt;
import com.solcoo.customer.Constant;
import com.solcoo.customer.base.task.HighAsyncTask;

public class Version extends BaseDO {

	private static String msg;

	public void setMsg(String msg) {
		Version.msg = msg;
	}

	public static boolean isNeedUpdate() {
		return StringUtil.isNotBlank(msg);
	}

	public static String getUrl() {
		return msg;
	}

	public static void check() {
		new VersionCheckTask().execute();
	}

	private static class VersionCheckTask extends
			HighAsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... params) {
			HttpResult result = HttpDownloader.getConnectData(Constant.URL
					.getVersion());
			if (result.isSuccess() && result.data != null
					&& result.data.length > 0) {
				JSON.parseObject(result.data, Version.class);
			}
			return null;
		}
	}

	public static void showDialog() {
		if (!isNeedUpdate()) {
			return;
		}

		if (!isNeedCheck()) {
			return;
		}

		DialogUtil.createDialog(
				ApplicationExt.getInstance().getCurrentContext(),
				onConfirmUpdateClickListener, DialogUtil.onNegativeListener,
				null, "亲，有新版本哦，赶快去升级吧！", "升级提示").show();
		setLastShowTime();
	}

	private static boolean isNeedCheck() {
		final long lastShowTime = getLastShowTime();
		return lastShowTime == 0
				|| Math.abs(DateUtil.getBetweenTime(System.currentTimeMillis(),
						lastShowTime)) > DateUtil.ONE_WEEK;
	}

	private static long getLastShowTime() {
		SharedPreferences localSharedPreferences = App.CONTEXT
				.getSharedPreferences(Constant.SharedKey.UPDATE_TIME,
						Context.MODE_PRIVATE);
		final long recordTime = localSharedPreferences.getLong(
				Constant.SharedKey.UPDATE_TIME, 0);
		return recordTime;
	}

	private static void setLastShowTime() {
		SharedPreferences localSharedPreferences = App.CONTEXT
				.getSharedPreferences(Constant.SharedKey.UPDATE_TIME,
						Context.MODE_PRIVATE);
		SharedPreferences.Editor localEditor = localSharedPreferences.edit();
		localEditor.putLong(Constant.SharedKey.UPDATE_TIME,
				System.currentTimeMillis());
		localEditor.commit();
	}

	private static OnClickListener onConfirmUpdateClickListener = new OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			dialog.dismiss();
			Intent it = new Intent(Intent.ACTION_VIEW, Uri.parse(msg));
			ApplicationExt.getInstance().getCurrentContext().startActivity(it);
		}
	};

}
